package com.example.currency;

import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

import java.util.concurrent.TimeUnit;

@Slf4j
public class MockHttpThread extends Thread {
    /**
     * https://www.cnblogs.com/yhlx/p/3498387.html
     */
    private static Interner<String> intern = Interners.newWeakInterner();
    private LoginUser loginUser;

    public MockHttpThread(String threadName, String loginToken) {
        setName(threadName);
        loginUser = new LoginUser();
        loginUser.setLoginToken(loginToken);
    }

    @Override
    @SneakyThrows
    public void run() {
        super.run();
        log.info("{}开始排队", getName());
        //synchronized (loginUser.getLoginToken()) {
        //synchronized (loginUser.getLoginToken().intern()) {
        synchronized (intern.intern(loginUser.getLoginToken())) {
            log.info("{}获取锁, 开始工作...", getName());
            TimeUnit.SECONDS.sleep(10);
            log.info("{}工作完成，释放锁", getName());
        }
    }
}
